package com.eastmoney.emis.edda.task;

import com.eastmoney.emis.datasource.redis.factory.RedisDbFactory;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.task
 * @author: 汪程程
 * @date: 2023/4/12 1:55 PM
 * @version: 1.0
 */
@Component
public class TaskContainer {

    public static final String REDIS_NAME = "trade";

    public boolean addTask(String taskId) {
        return Boolean.TRUE.equals(RedisDbFactory.getStringRedisTemplate(REDIS_NAME).opsForValue()
                .setIfAbsent(makeKey(taskId), taskId, Duration.ofSeconds(30)));
    }

    public boolean removeTask(String taskId) {
        return Boolean.TRUE.equals(RedisDbFactory.getStringRedisTemplate(REDIS_NAME)
                .expire(makeKey(taskId), Duration.ofSeconds(1)));
    }


    private String makeKey(String id) {
        return "EDDA:HSBC:" + id;
    }
}
